<template>
  <el-dialog
    :visible.sync="dialogVisible"
    title="枚举值编辑"
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    :before-close="beforeClose"
    class="tag-edit-dialog tag-enums-dialog"
    width="30%"
    v-dialogMove
  >
    <el-form :model="editForm" ref="editForm">
      <el-row class="title" :gutter="10">
        <el-col :span="10" align="center">枚举值</el-col>
        <el-col :span="1" align="center"></el-col>
        <el-col :span="13" align="center">描述</el-col>
      </el-row>
      <el-row v-for="(item, index) in editForm.list" :key="index" :gutter="10">
        <el-col :span="10">
          <el-form-item
            :prop="'list.' + index + '.key'"
            :rules="{
              required: true,
              message: '枚举值不能为空',
              trigger: 'blur'
            }"
          >
            <el-input v-model="item.key" placeholder="请输入枚举值"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="1" align="center">：</el-col>
        <el-col :span="13">
          <el-form-item
            :prop="'list.' + index + '.value'"
            :rules="{
              required: true,
              message: '描述不能为空',
              trigger: 'blur'
            }"
          >
            <el-input v-model="item.value" placeholder="请输入枚举描述"></el-input>
          </el-form-item>
        </el-col>
        <div class="btns">
          <el-button type="primary" icon="el-icon-plus" size="mini" circle @click="addParams(index)"></el-button>
          <el-button
            type="warning"
            icon="el-icon-minus"
            size="mini"
            circle
            @click="remove(index)"
            v-if="index !== 0"
          ></el-button>
        </div>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="beforeClose">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      index: 0,
      editForm: {
        list: []
      }
    }
  },
  methods: {
    showDialog(info) {
      this.index = info.index
      let enums = info.optParamVal.split('|')
      enums.forEach(item => {
        this.editForm.list.push({ key: item.split(':')[0], value: item.split(':')[1] })
      })
      this.dialogVisible = true
    },
    addParams(index) {
      this.editForm.list.splice(index + 1, 0, { key: '', value: '' })
    },
    remove(index) {
      this.editForm.list.splice(index, 1)
    },
    submit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          let result = []
          let sameTag = new Set()
          this.editForm.list.forEach(item => {
            sameTag.add(item.key.trim())
            result.push(item.key + ':' + item.value)
          })
          if (sameTag.size !== this.editForm.list.length) {
            this.$message.error('枚举值不能重复')
            return
          }
          this.$emit('updateParams', result.join('|'), this.index)
          this.beforeClose()
        }
      })
    },
    beforeClose() {
      this.editForm.list = []
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss">
.tag-enums-dialog {
  .el-form {
    margin-top: 30px;
  }
  .title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 10px;
  }
  .el-row {
    position: relative;
    padding-right: 75px;
    .el-col-1 {
      line-height: 35px;
    }
    .btns {
      width: 66px;
      position: absolute;
      top: 0;
      right: 5px;
      .el-button {
        margin-top: 5px;
      }
    }
  }
}
</style>
